/*
 * --------------------------------------------------------------
 * Copyright ©  2015 Hangzhou DtDream Technologies Co.,Ltd.
 * --------------------------------------------------------------
 *      Product: 协同数治
 *  Module Name: design
 * Date Created: 2022/12/16
 *  Description: TODO
 * --------------------------------------------------------------
 * Modification History
 * DATE            NAME            Description
 * --------------------------------------------------------------
 * 2022/12/16       yewj            TODO
 * --------------------------------------------------------------
 */

package com.dtdream.pattern.single.type5;

/**
 * 双重检查
 * 1、
 *
 * @Author: yewj
 **/
public class SingleFive {
    public static void main(String[] args) {

        Singleton instanceOne = Singleton.getInstance();
        Singleton instanceTwo = Singleton.getInstance();
        System.out.println(instanceOne == instanceTwo);
        System.out.println("instanceOne.hashCode() = " + instanceOne.hashCode());
        System.out.println("instanceTwo.hashCode() = " + instanceTwo.hashCode());
    }
}

class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    // 提供一个静态的公有方法，加入了同步处理的代码，解决线程安全问题
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
            instance = new Singleton();
        }
        return instance;
    }
}
